/*******************************************************************************
 * echidna - A framework for event based reactive systems
 * Copyright (C) 2012 Daniele Pallastrelli 
 *
 *
 * This file is part of echidna.
 * For more information, see http://echidna.googlecode.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#ifndef ECHIDNA_EVENTMIRROR_H_
#define ECHIDNA_EVENTMIRROR_H_

#include <boost/shared_ptr.hpp>
#include "eventbus.h"

namespace echidna
{

template < typename E, typename S >
class EventMirror
{
public:
    EventMirror( boost::shared_ptr< EventBus > mb ) :
        msgBroker( mb )
    {
        subscription = mb -> Subscribe< E >( boost::bind( &EventMirror< E, S >::Send, this, _1 ) );
    }
    ~EventMirror()
    {
        subscription.Disconnect();
    }
    void StreamReceived( const std::string& s )
    {
        E e = S::FromStream( s );
        msgBroker -> Post( e );
    }
    void SendStream( const std::string& toSend )
    {
        std::cout << "--> " << toSend << std::endl;
    }
private:
    void Send( const E& event )
    {
        std::string s = S::ToStream( event );
        SendStream( s );
    }
    boost::shared_ptr< EventBus > msgBroker;
    EventBus::Subscription subscription;
};

} // namespace echidna

#endif // ECHIDNA_EVENTMIRROR_H_
